<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sermon extends CH_Controller{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	 /**
         * Constructor
         */
	function __construct() { 
		parent::__construct(); 
		$this->init();
		//$this->load->helper(array('form','array', 'url','common','login','ckeditor'));
		$this->load->model(array('common_model','sermon_model','setting_model'));
    } 
    	
	/**
	 * 설교방송 목록
	 *
	 * @access	public
	 * @return	result
	 */
	function sermonList(){
		login_admin();
		$url = $this->uri->segment(1).'/'.$this->uri->segment(2);
		$uri_array = segment_explode($this->uri->uri_string());
		$array_segment = array_keys($this->uri->segment_array(), "page");
		$page = in_array('page', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'page'))) : 1;
		$uri_segment = in_array('page', $uri_array) ? $array_segment[0]+1 : 0;
		$keyname = urldecode($this->security->xss_clean(url_explode($uri_array, 'keyname')));
		$keyword = urldecode($this->security->xss_clean(url_explode($uri_array, 'keyword')));
		$param['keyname'] = $keyname;
		$param['keyword'] = $keyword;
		$param['page'] = $page;
		
		$listnum = $this->config->item('ch_listnum');
		
		$count = $this->sermon_model->list_total($param);
		$result = $this->sermon_model->sermon_list($param);
		
		$this->load->library('pagination');
		$config['base_url'] = '/sermon/sermonList';
		$config['total_rows'] = $count;
		$config['per_page'] = $listnum;
		$config['use_page_numbers'] = true;
		
		$config['uri_segment'] = $uri_segment;
		$this->pagination->initialize($config);
		
		
		$list_no = $count - $listnum * ($page-1);
		$data = array(
				'_title' => '설교방송관리',
				'_group' => 'homepage',
				'_content' => $this->config->item('ch_manager_root').'/'.$url,
				'rst' => $result,
				'paging'=> $this->pagination->create_links(),
				'list_no'=> $list_no,
				'page'=>$page,
				'siteinfo'=> $this->setting_model->select_siteinfo()
		);
		
		
		
		/*$url = $this->uri_church();
		$data = array(
				'_title' => '설교방송관리',
				'_content' => $this->config->item('ch_manager_root').'/'.$url
		);*/	
		return $data ;
	}
	
	/**
	 * 설교방송 폼
	 *
	 * @access	public
	 * @return	result
	 */
	function sermonForm(){
		login_admin();
		$uri_array = segment_explode($this->uri->uri_string());
		$page = in_array('page', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'page'))) : 1;
		$sermon_id = in_array('id', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'id'))) : 1;
		$url = $this->uri->segment(1).'/'.$this->uri->segment(2);
		
		$ckeditor = array('path'=> 'asset/ckeditor','id'=>'textarea_id','config'=>array('filebrowserImageUploadUrl'=>'/asset/ckeditor/upload.php?type=Images'));
		
		$data = array(
				'_title' => '설교방송관리',
				'_group' => 'homepage',
				'_content' => $this->config->item('ch_manager_root').'/'.$url,
				'_ckeditor' => $ckeditor
				
		);
		//serise 마스트코드 : 1
		$data = array_merge($data,array('serise'=> $this->common_model->select_child_code(1)),$this->sermon_model->select($sermon_id ));
		return $data ;
	}
	
	/**
	 * 설교방송 db 처리
	 *
	 * @access	public
	 * @return	void
	 */
	function sermonDbjob(){
		$dbjob = $this->input->post('dbjob');
		$content = $this->input->post('content');
		$title = $this->input->post('title');
		
		if(!get_magic_quotes_gpc()){
			$content = addslashes($content);
			$title = addslashes($title);
		}
	
		switch($dbjob){
			case('i'):
				$msg = '저장되었습니다.';
				$data = array(
						'series' => $this->input->post('series') ,
						'airdate' => $this->input->post('airdate') ,
						'worship' => $this->input->post('worship') ,
						'runtime' => $this->input->post('runtime'),
						'bible'=> $this->input->post('bible'),
						'preacher'=> $this->input->post('preacher'),
						'vod_url'=> $this->input->post('vod_url'),
						'open_yn'=> $this->input->post('open_yn'),
						'title'=> $title,
						'content'=> $content,
						'reg_date'=> date("Y-m-d H:i",time()),
						'reg_uid'=> 'admin'
				);
			break;
			case('u'):
				$msg = '수정되었습니다.';
				$data = array(
						'series' => $this->input->post('series') ,
						'airdate' => $this->input->post('airdate') ,
						'worship' => $this->input->post('worship') ,
						'runtime' => $this->input->post('runtime'),
						'bible'=> $this->input->post('bible'),
						'preacher'=> $this->input->post('preacher'),
						'vod_url'=> $this->input->post('vod_url'),
						'open_yn'=> $this->input->post('open_yn'),
						'title'=> $title,
						'content'=> $content,
						'mod_date'=> date("Y-m-d H:i",time()),
						'mod_uid'=> 'admin'
				);
			break;
			case('d'):
				$msg = '삭제 되었습니다.';
				$data = array('del_yn'=>'Y');
			break;
		}
		
		if(!empty($_FILES['file1']['tmp_name'])){
			$this->load->library('upload',array('allow_file'=>null,'allow_size'=> null,'file'=>'file1','upload_path'=>'sermon'));
			$this->upload->fileUpload();
			$file = $this->upload->getUploadResult();
			$data = array_merge($data, array('list_image'=> $file['real_file']));
		}
		
		$this->sermon_model->dbjob($data);
		$this->alert->jalert($msg, base_url('/sermon/sermonList'));
	}
	
	
}

/* End of file html.php */
/* Location: ./application/controllers/member.php */